home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / QuickTime / Show Movie / Sources / MenuStuff.c < prev    next >
Encoding:
Text File  |  1995-11-20  |  11.7 KB  |  453 lines  |  [TEXT/MMCC]

  1. /*
  2.   File:            MenuStuff.c
  3.   Contains:        Menu handling routines.
  4.   Written by:    Jason Hodges-Harris & Don Swatman
  5.   Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  6. */
  7.  
  8. //==============================================
  9. // MenuStuff.c
  10. //
  11. // Creates and destroys the menu bar
  12. // Responds to user selecting menu item (mouse or keyboard)
  13. // Does all the calls to open a single movie or a master
  14. // and slave 
  15. //==============================================
  16.  
  17.  
  18. #include <Fonts.h>
  19. #include <Devices.h>
  20. #include <ToolUtils.h>
  21.  
  22. #include "MenuStuff.h"
  23.  
  24. #include "WindStuff.h"
  25. #include "MovieStuff.h"
  26. #include "MoviePrefs.h"
  27.  
  28. //----------------------------------------------
  29. // Define menubar constants
  30. //----------------------------------------------
  31.  
  32. #define    kMenuBar        128
  33.  
  34. #define mApple    128
  35. #define iAbout            1
  36.  
  37. #define    mFile    129
  38. #define iOpen             1
  39. #define iOpenMS         2
  40. #define iClose     3
  41. #define iQuit      5
  42.  
  43. #define mEdit   130
  44. #define iUndo            1
  45. #define iCut            3
  46. #define    iCopy            4
  47. #define iPaste        5
  48.  
  49.  
  50. //----------------------------------------------
  51. // Create the menu bar
  52. //----------------------------------------------
  53.  
  54. void MenuBarInit (void)
  55. {
  56.     Handle        menuBar;
  57.     MenuHandle    hMenu;
  58.     
  59. // Create the menu bar
  60.     menuBar = GetNewMBar(kMenuBar);  // Get a menu bar resource 
  61.     SetMenuBar (menuBar);            // Set the current menu bar to it 
  62.  
  63.     hMenu = GetMHandle(mApple); // Get a handle to the apple Menu
  64.     AddResMenu (hMenu,'DRVR');  // Attach the Desk Accessories on to the end
  65.     
  66.     DrawMenuBar();    // Ok, draw the menu bar
  67. }
  68.  
  69. //==============================================
  70. // Various menu bar utilities
  71. //==============================================
  72.  
  73. //----------------------------------------------
  74. // SetMenuItemEnabled
  75. //
  76. // Grays or Un grays a menu item
  77. // Returns true if something was enabled
  78. //----------------------------------------------
  79.  
  80. Boolean SetMenuItemEnabled ( short menuID,
  81.                                                          short theItem,
  82.                                                          Boolean enableState );
  83. Boolean SetMenuItemEnabled ( short menuID,
  84.                                                          short theItem,
  85.                                                          Boolean enableState )
  86. {
  87.     MenuHandle    hMenu;
  88.  
  89.     hMenu = GetMHandle(menuID);
  90.     if (enableState)
  91.         EnableItem(hMenu, theItem);
  92.     else
  93.         DisableItem(hMenu, theItem);
  94.     return ( enableState );
  95. }
  96.  
  97. //----------------------------------------------
  98. // DoAdjustFileMenu
  99. //
  100. // Sets each item in the file menu
  101. //----------------------------------------------
  102.  
  103. static Boolean    DoAdjustFileMenu(WindowPtr window);
  104. static Boolean    DoAdjustFileMenu(WindowPtr window)
  105. {
  106.     short newWind;          // throw away value used by IsFreeWind
  107.     Boolean menuEnabled;    // true if any item is enabled
  108.     Boolean redrawMenuBar;  // true if we need to redraw the menu bar
  109.     static
  110.             Boolean    fileMenuEnabled = true; // This is a static so it'll hang around
  111.                                                                             // Reflects whither the file menu is currently
  112.                                                                             // grayed(false) or enabled(true)
  113.  
  114. // Open needs just one free window 
  115.     menuEnabled  = SetMenuItemEnabled( mFile, iOpen,   IsFreeWind(&newWind, 1) );
  116.  
  117. // Open master and slave needs 2 free windows
  118.     menuEnabled |= SetMenuItemEnabled( mFile, iOpenMS, IsFreeWind(&newWind, 2) );
  119.  
  120. // Close is ok if it's our window
  121.     menuEnabled |= SetMenuItemEnabled( mFile, iClose,  IsOurWindow( window ) );
  122.  
  123. // Quit always enabled
  124.     menuEnabled |= SetMenuItemEnabled( mFile, iQuit,   true);
  125.  
  126. // If nothing enabled the change then menu title to grayed out
  127.     if (!menuEnabled)
  128.         menuEnabled = SetMenuItemEnabled( mFile, 0,  false);
  129.     
  130. // If menu title has changed, then return the fact that we have
  131. //   to redraw the menu bar
  132.     redrawMenuBar   = (fileMenuEnabled != menuEnabled);
  133.     fileMenuEnabled = menuEnabled;
  134.  
  135.     return(redrawMenuBar);
  136. }
  137.  
  138. //----------------------------------------------
  139. // DoAdjustEditMenu
  140. //
  141. // As we don't do use this menu, we just gray
  142. // the whole lot out
  143. //----------------------------------------------
  144.  
  145. static Boolean    DoAdjustEditMenu(WindowPtr window )
  146. {
  147. #pragma unused ( window )
  148.  
  149.     Boolean            menuEnabled;    // true if any item is enabled
  150.     Boolean            redrawMenuBar;  // true if we need to redraw the menu bar
  151.     short              itemCount;      // count down each menu item
  152.     static 
  153.             Boolean    editMenuEnabled = true; // This is a static so it'll hang around
  154.                                                                             // Reflects whither the file menu is currently
  155.                                                                             // grayed(false) or enabled(true)
  156.  
  157. // Gray out all the items
  158.     menuEnabled = false;
  159.     for (itemCount = iUndo; itemCount <= iPaste; ++itemCount)
  160.         menuEnabled |= SetMenuItemEnabled(mEdit, itemCount, false);
  161.  
  162. // If nothing enabled then change the menu title to grayed out
  163.     if (!menuEnabled)
  164.         menuEnabled |= SetMenuItemEnabled( mEdit, 0,  false);
  165.  
  166. // If menu title has changed, then return the fact that we have
  167. //   to redraw the menu bar
  168.     redrawMenuBar   = (editMenuEnabled != menuEnabled);
  169.     editMenuEnabled = menuEnabled;
  170.     return(redrawMenuBar);
  171. }
  172.  
  173. //----------------------------------------------
  174. // DoAdjustMenus
  175. //
  176. // Adjust all the menu items and redraws the menu 
  177. // bar if something has changed
  178. //----------------------------------------------
  179.  
  180. void    DoAdjustMenus(void)
  181. {
  182.     WindowPtr    window;
  183.     Boolean   redrawMenuBar;
  184.  
  185.     window = FrontWindow();
  186.  
  187.     redrawMenuBar  = DoAdjustFileMenu ( window );  // Adjust the file menu
  188.     redrawMenuBar |= DoAdjustEditMenu ( window );  // Adjust the edit menu
  189.  
  190. // redraw the menu bar if one of the menu titles has changed
  191.     if (redrawMenuBar)
  192.         DrawMenuBar();
  193. }
  194.  
  195.  
  196. //----------------------------------------------
  197. // DoSlaveMovieTest
  198. //
  199. // Open two movies and slave one of the other
  200. //----------------------------------------------
  201.  
  202. void DoSlaveMovieTest ( void );
  203. void DoSlaveMovieTest ( void )
  204. {
  205.     OSErr   theErr = noErr;
  206.     short   masterNum = -1;     // window number of master window
  207.     short   slaveNum  = -1;     // window number of slave window
  208.  
  209. // Check to see if there is room for 2 movies
  210.     if (IsFreeWind(&masterNum, 2))
  211.     {
  212. // Let the user change any of the preferences
  213.         theErr = OneMoviePref( &gDefaultMoviePrefs, true );
  214.         if (!theErr)
  215.         {
  216.     
  217.     // Create the master movie in the master Window
  218.             CreateWindow( masterNum, "\pMaster Movie", kWindAtFront );
  219.             if (gTheWinds[masterNum])
  220.             {
  221.     // Create a movie
  222.                 theErr = OpenMovieWindow ( gTheWinds[masterNum],  // WindowPtr
  223.                                                                      kUserCloseWind,        // Force user to close at end
  224.                                                                      gDefaultMoviePrefs.hasController ); // Add controller if
  225.                                                                                                                                               // user wants it
  226.                 if (!theErr)
  227.                 {
  228.     
  229.     // Create the slave movie Window
  230.                     if (IsFreeWind(&slaveNum, 1))
  231.                     {
  232.                         CreateWindow( slaveNum, "\pSlave Movie", gTheWinds[masterNum] );
  233.                         if (gTheWinds[slaveNum])
  234.                         {
  235.     // Create a movie in the slave window
  236.                             theErr = OpenMovieWindow ( gTheWinds[slaveNum],  // WindowPtr
  237.                                                                                  kUserCloseWind,       // Force user to close at end
  238.                                                                                  kNoMovieController ); // No Movie Controller
  239.     
  240.     // Set up any changes to the masters movies rate
  241.                             if (!theErr)
  242.                                 if (gDefaultMoviePrefs.rateChangeDelay)
  243.                                     theErr = SetupMovieRate( gTheWinds[masterNum],  gDefaultMoviePrefs.rateChangeDelay  );
  244.                                 
  245.     // Set up the looping in the master movie - 20 seconds from start go back to 10 secs
  246.                             if (!theErr)
  247.                                 if (gDefaultMoviePrefs.do20to10Loop)
  248.                                     theErr = SetupLoop( gTheWinds[masterNum], 20, 10  );
  249.                 
  250.     // Now hook the two movies together
  251.                             if (!theErr)
  252.                                 theErr = SetupSlaveMovie( gTheWinds[masterNum],
  253.                                                                                     gTheWinds[slaveNum],
  254.                                                                                     gDefaultMoviePrefs.slaveAheadBy,
  255.                                                                                     gDefaultMoviePrefs.slaveStartDelay );
  256.  
  257.     // Display and order the windows
  258.                             if (!theErr)
  259.                             {
  260.                 // Show the windows
  261.                                 ShowWindow( gTheWinds[slaveNum] );
  262.                                 ShowWindow( gTheWinds[masterNum] );
  263.                 
  264.                 // Select the master window (as the front active window)
  265.                                 SelectWindow ( gTheWinds[masterNum] );
  266.             
  267.                 // Move slave window to be behind the master in the window list
  268.                                 SendBehind (gTheWinds[slaveNum], gTheWinds[masterNum]);
  269.  
  270.                             }    
  271.  
  272.                 // If we don't have a controller then start the movie
  273.                             if (!theErr)
  274.                                 if (!gDefaultMoviePrefs.hasController)
  275.                                     theErr = StartMovieWindow( gTheWinds[masterNum],kStartFromBegining );
  276.             
  277.                         }
  278.                     }
  279.                 }
  280.  
  281. // If we have an error, then close down the windows
  282.                 if (theErr)
  283.                 {
  284.                     if (masterNum != -1)
  285.                         CloseOurWindow( masterNum );
  286.                     if (slaveNum != -1)
  287.                         CloseOurWindow( slaveNum );
  288.                 }
  289.             }
  290.     // Adjust the menus, whatever happens
  291.             DoAdjustMenus();
  292.         }
  293.     }
  294. }
  295.  
  296.  
  297. //----------------------------------------------
  298. // DoOpenWindow
  299. //
  300. // Open one movie using the options setup
  301. //----------------------------------------------
  302.  
  303. void DoOpenWindow (void);
  304. void DoOpenWindow (void)
  305. {
  306.     OSErr   theErr;
  307.     short   windNum;    // The windows number in the array
  308.         
  309. // Check there is a free window
  310.     if (IsFreeWind(&windNum,1))
  311.     {
  312. // Get any options the user might want
  313.         theErr = OneMoviePref( &gDefaultMoviePrefs, false );
  314.         if (!theErr)
  315.         {
  316.  
  317. // Open a window
  318.             CreateWindow( windNum, "\pUntitled", kWindAtFront  );
  319.             if (gTheWinds[windNum])
  320.             {
  321.     // Create a movie
  322.                 theErr = OpenMovieWindow ( gTheWinds[windNum],                 // windowPtr
  323.                                                                      gDefaultMoviePrefs.closeAtEnd,      // Users option for auto close
  324.                                                                      gDefaultMoviePrefs.hasController ); // Users Option for controller
  325.                 
  326.     // Set up the movie rate changes
  327.                 if (!theErr)
  328.                     if (gDefaultMoviePrefs.rateChangeDelay)
  329.                         theErr = SetupMovieRate( gTheWinds[windNum],  gDefaultMoviePrefs.rateChangeDelay  );
  330.                     
  331.     // Set up the looping in the master movie - 20 seconds from start go back to 10 secs
  332.                 if (!theErr)
  333.                     if (gDefaultMoviePrefs.do20to10Loop)
  334.                         theErr = SetupLoop( gTheWinds[windNum],
  335.                                                                 gDefaultMoviePrefs.loopFrom, gDefaultMoviePrefs.loopTo  );
  336.                     
  337. // if no errors, then display the windows
  338.                 if (!theErr)
  339.                 {
  340.         // Show the windows
  341.                     ShowWindow(gTheWinds[windNum]);
  342.     
  343.         // Select the window (as the front active window)
  344.                     SelectWindow ( gTheWinds[windNum] );
  345.                 }    
  346.  
  347.     // If we don't have a controller then start the movie
  348.                 if (!theErr)
  349.                     if (!gDefaultMoviePrefs.hasController)
  350.                         theErr = StartMovieWindow( gTheWinds[windNum],kStartFromBegining );
  351.     
  352.     // If we've had an error, then remove the window and clean up
  353.                 if (theErr)
  354.                 {
  355.                     if (windNum != -1)
  356.                         CloseOurWindow( windNum );
  357.                 }
  358.             }    
  359.         }
  360. // Adjust the menus, whatever happens
  361.         DoAdjustMenus();
  362.     }
  363. }
  364.  
  365.  
  366. //----------------------------------------------
  367. // DoMenuCommand
  368. //
  369. // Handles selection of items in menu
  370. //----------------------------------------------
  371.  
  372. void    DoMenuCommand(long menuResult)
  373. {
  374.     short        menuID;
  375.     short        menuItem;
  376.     short        daRefNum;
  377.     Str255    daName;
  378.     short   windNum;
  379.     
  380. // convert menuResult into menu and item
  381.     menuID   = HiWord(menuResult);
  382.     menuItem = LoWord(menuResult);
  383.  
  384.     switch (menuID) 
  385.     {
  386.  
  387. // Apple Menu
  388.         case mApple:
  389.             switch (menuItem) 
  390.             {
  391.     // Display about box
  392.                 case iAbout:
  393.                     AboutBox();
  394.                     break;
  395.  
  396.     // Handle DA selected in Apple Menu
  397.                 default:
  398.                     GetItem(GetMHandle(mApple), menuItem, daName);
  399.                     daRefNum = OpenDeskAcc(daName);
  400.                     break;
  401.             }
  402.             break;
  403.  
  404. // File Menu
  405.         case mFile:
  406.             switch (menuItem) 
  407.             {
  408.  
  409.         // Open a movie window
  410.                 case iOpen:
  411.                     DoOpenWindow ();
  412.                     break;
  413.  
  414.         // Open a master and slave movie windows
  415.                 case iOpenMS:
  416.                     DoSlaveMovieTest( );                    
  417.                     break;
  418.         // Close front window
  419.                 case iClose:
  420.                     windNum = GetWindowNum ( FrontWindow() );
  421.                     if (windNum != -1 )
  422.                         CloseOurWindow( windNum );
  423.                     break;
  424.         // User wants to quit so set gDone to True
  425.                 case iQuit:
  426.                     gDone=true;     
  427.                     break;
  428.             }
  429.             break;
  430.  
  431. // Edit Menu
  432.     // Don't do anything
  433.         case mEdit:
  434.             switch (menuItem) 
  435.             {
  436.                 case iUndo:
  437.                     break;
  438.                 case iCut:
  439.                     break;
  440.                 case iCopy:
  441.                     break;
  442.                 case iPaste:
  443.                     break;
  444.             }
  445.         break;
  446.             
  447.     }
  448. // Un highlite the menu title
  449.     HiliteMenu(0);
  450. }
  451.  
  452.  
  453.